﻿using System;
using System.Threading;
using System.Globalization;
using Kiva7.Constants;
using Kiva7.Settings;
using Kiva7.TranslatorService;

namespace Kiva7.Translations
{
    public class TranslatorContext
    {
        private TranslatorContext()
        {
            _proxy = new LanguageServiceClient();
            _proxy.TranslateCompleted += TranslateCompleted;
            _proxy.DetectCompleted += _proxy_DetectCompleted;
            //_proxy.SpeakCompleted+= SpeakCompleted;
        }

        void _proxy_DetectCompleted(object sender, DetectCompletedEventArgs e)
        {
            if (e.Error != null
                || e.Cancelled)
            {
                return;
            }
                Translation text = e.UserState as Translation;
                if (text != null)
                {
                    string code = LanguageCode();
                    if (string.Compare(code, e.Result, StringComparison.CurrentCultureIgnoreCase) != 0)
                    {
                        Current._proxy.TranslateAsync(Constant.BingAppId, text.Original, e.Result, code, "text/plain",
                                                      "General", text);
                    }
                }
        }

        // http://www.codeproject.com/Articles/136164/Bing-Translator-service-for-Windows-Phone-7.aspx

        public static IFormatProvider NumberFormat
        {
            get
            {
                return CultureInfo.InvariantCulture;
            }
        }
        private readonly LanguageServiceClient _proxy;

        public static string LanguageCode()
        {
            if (Thread.CurrentThread.CurrentCulture.Parent != null)
            {
                return Thread.CurrentThread.CurrentCulture.Parent.TwoLetterISOLanguageName;
            }
            return Thread.CurrentThread.CurrentCulture.TwoLetterISOLanguageName;
        }

        public static void TranslateTextAutomatic(Translation text)
        {
            if (string.IsNullOrEmpty(text.Original))
            {
                return;
            }

            string code = LanguageCode();
            if (string.Compare(code, "en", StringComparison.CurrentCultureIgnoreCase) != 0
                && AppSettings.Current.TranslationSetting == TranslatorOption.Automatic)
            {
                Current._proxy.TranslateAsync(Constant.BingAppId, text.Original, "en", code, "text/plain", "General",
                                              text);
            }
        }

        public static void TranslateText(Translation text)
        {
            if (string.IsNullOrEmpty(text.Original))
            {
                return;
            }
            string code = LanguageCode();
            if (string.Compare(code, "en", StringComparison.CurrentCultureIgnoreCase) != 0)
            {
                Current._proxy.TranslateAsync(Constant.BingAppId, text.Original, "en", code, "text/plain", "General",
                                              text);
            }
        }

        public static void TranslateTextWithDetect(Translation text)
        {
            if (string.IsNullOrEmpty(text.Original))
            {
                return;
            }
            Current._proxy.DetectAsync(Constant.BingAppId, text.Original, text);
        }


        private static void TranslateCompleted(object sender, TranslateCompletedEventArgs e)
        {
            if (e.Error!=null)
            {
                
            }
            if (e.Cancelled)
            {

            }
            else
            {
                Translation text = e.UserState as Translation;
                if (text != null)
                {
                    text.Translated = e.Result;
                }
            }
        }

        private static TranslatorContext _current;
        public static TranslatorContext Current
        {
            get
            {
                if (_current == null)
                {
                    _current = new TranslatorContext();
                }
                return _current;
            }
        }
    }
}
